﻿using System.Runtime.Serialization;
using System.Windows.Media;

using Game.Common.Resources;
using Game.Terrain;

namespace Game.Types
{
    /// <summary>
    /// The class describing properties that are common 
    /// to all vehicles of a certain kind.
    /// </summary>
    [DataContract(Namespace="game")]
    public class VehicleType : ContainerBaseType, IHasCost
    {
        int _FuelConsumption;
        int _Armor;
        int _TargetingAccuracy;
        ImageSource _InfoImage;
        ImageSource _InfoImageSmall;
        WeaponType _WeaponType;
        ReadonlyGameResources _Tank;
        ReadOnlyGameMovement _Movement;
        ReadOnlyTerrainAccess _TerrainAccess;
        ReadonlyGameResources _Cost;
        
        internal VehicleType()
        {
        }
        
        [OnDeserialized]
        public void OnDeserialized2(StreamingContext c)
        {
            object obj = ResourceLocator.GameResourceLocator.Get(Url);
            var t = obj as VehicleType;
            if (t != null)
            {
                _InfoImage = t.InfoImage;
                _InfoImageSmall = t.InfoImageSmall;
                _WeaponType = t.WeaponType;
                _Tank = t.Tank;
                _Movement = t.Movement;
                _TerrainAccess = t.TerrainAccess;
                _FuelConsumption = t.FuelConsumption;
                _Armor = t.Armor;
                _TargetingAccuracy = t.TargetingAccuracy;
                _Cost = t.Cost;
            }
        }
        
        public VehicleType(VehicleTypeBuilder builder)
            : base(builder)
        {
            _InfoImage = builder.InfoImage;
            _InfoImageSmall = builder.InfoImageSmall;
            _Tank = builder.Tank.GetReadOnly();
            _FuelConsumption = builder.FuelConsumption;
            _TargetingAccuracy = builder.TargetingAccuracy;
            _Armor = builder.Armor;
            _Movement = builder.Movement.GetReadOnly();
            _WeaponType = new WeaponType();
            _TerrainAccess = builder.TerrainAccess.ToReadOnly();
            _Cost = builder.Cost.GetReadOnly();
        }
        
        public ImageSource InfoImage
        {
            get { return _InfoImage; }
        }
        
        public ImageSource InfoImageSmall
        {
            get { return _InfoImageSmall; }
        }
       
        public ReadonlyGameResources Tank
        {
            get { return _Tank; }
        }

        public ReadOnlyGameMovement Movement
        {
            get { return _Movement; }
        }

        public ReadonlyGameResources Cost
        {
            get { return _Cost; }
        }

        public WeaponType WeaponType
        {
            get { return _WeaponType; }
        }
        
        /// <summary>
        /// the fuel consumption to move a single field
        /// </summary>
        public int FuelConsumption
        {
            get { return _FuelConsumption; }
        }
        
        public int Armor
        {
            get { return _Armor; }
        }

        public int TargetingAccuracy
        {
            get { return _TargetingAccuracy; }
        }
        
        public ReadOnlyTerrainAccess TerrainAccess
        {
            get { return _TerrainAccess; }
        }

        public override string ToString()
        {
            return Name;
        }

    }

#region "Builder"
    public class VehicleTypeBuilder : ContainerBaseTypeBuilder
    {
        readonly WeaponTypeCollection _Weapons = new WeaponTypeCollection();
        readonly TerrainAccess _TerrainAccess = new TerrainAccess();

        GameResources _Tank = new GameResources();
        GameResources _Cost = new GameResources();

        public VehicleTypeBuilder()
        {
            TargetingAccuracy = 100;
            _TerrainAccess.Terrain.Add(TerrainBitTypes.Mainland);
            _TerrainAccess.TerrainNot.Add(TerrainBitTypes.Water);
            Movement = new GameMovementBuilder();
        }
        
        public int FuelConsumption {get; set;}
        
        public int Armor {get; set;}
        
        public int TargetingAccuracy { get; set; }
            
        public ImageSource InfoImage {get; set;}
        
        public ImageSource InfoImageSmall {get; set;}
        
        public GameResources Tank 
        {
            get { return _Tank; }
            set { _Tank=value; }
        }

        public GameResources Cost
        {
            get { return _Cost; }
            set { _Cost = value; }
        }

        public GameMovementBuilder Movement { get; set; }

        public TerrainAccess TerrainAccess 
        {
            get { return _TerrainAccess; }
        }
        
        public VehicleType BuildVehicleType()
        {
            return new VehicleType(this);
        }
        
        public WeaponTypeCollection Weapons
        {
            get { return _Weapons; }
        }
        
        public override string TypeName 
        {
            get { return "vehicle"; }
        }
        
    }
#endregion

}
